package cn.tedu.luban.settle.main.converter;

import cn.tedu.luban.order.protocol.dto.OrderMessageDTO;
import cn.tedu.luban.settle.protocol.dos.SettleBillDO;
import cn.tedu.luban.settle.protocol.param.SettleBillParam;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

@Component
public class SettleConverter {
    public List<SettleBillParam> dto2params(OrderMessageDTO order) {
        SettleBillParam workerParam=new SettleBillParam();
        BeanUtils.copyProperties(order,workerParam);
        workerParam.setOrderGmtCreate(order.getGmtCreate());
        workerParam.setType(1);
        workerParam.setRequestUserPhone(order.getUserPhone());
        workerParam.setWorkerId(order.getUserId());
        //没有userId
        workerParam.setRequestUserId(1000L);
        workerParam.setRequestUserName(order.getUserName());
        workerParam.setScale(order.getProfitScale());
        workerParam.setAmount(order.getRequestOrderPrice());
        workerParam.setTotalAmount(order.getRequestOrderPrice());
        workerParam.setStatus(1);
        Long payTime=System.currentTimeMillis();
        workerParam.setPaymentTime(payTime);
        //平台
        SettleBillParam plateParam=new SettleBillParam();
        BeanUtils.copyProperties(workerParam,plateParam);
        plateParam.setUserId(999L);
        plateParam.setWorkerId(999L);
        plateParam.setType(2);
        plateParam.setAmount(order.getRequestOrderRawPrice()==null? 100L- workerParam.getRequestOrderPrice():order.getRequestOrderRawPrice()- workerParam.getRequestOrderPrice());
        plateParam.setTotalAmount(plateParam.getAmount());
        List<SettleBillParam> settles=new ArrayList<>();
        settles.add(workerParam);
        settles.add(plateParam);
        return settles;
    }

    public List<SettleBillDO> params2dos(List<SettleBillParam> params) {
        if (CollectionUtils.isEmpty(params)){
            return Collections.emptyList();
        }
        List<SettleBillDO> list = new ArrayList<>();
        params.forEach(settleBillParam -> {
            SettleBillDO settleBill = param2do(settleBillParam);
            list.add(settleBill);
        });
        return list;
    }
    public SettleBillDO param2do(SettleBillParam param) {
        SettleBillDO settleBill = new SettleBillDO();
        if (param==null){
            return settleBill;
        }
        BeanUtils.copyProperties(param, settleBill);
        settleBill.setRequestOrderPrice(param.getRequestOrderRawPrice());
        settleBill.setGmtCreate(System.currentTimeMillis());
        settleBill.setGmtModified(System.currentTimeMillis());
        settleBill.setCreateUserId(999L);
        settleBill.setModifiedUserId(999L);
        settleBill.setCreateUserName("SYSTEM");
        settleBill.setModifiedUserName("SYSTEM");
        return settleBill;
    }
}
